package draw;

import classifiers.CircleClassifier;
import histogram.HistogramDescr;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

public class ImageFromArray {

//    static int[] circleColor = {255, 0, 0, 255};
    static int[] foneColor = {255, 255, 255, 255};

    public static BufferedImage createImageFromArray(int[][] binImage) {

        int width = binImage.length;
        int height = binImage[0].length;
        if (height > width) {
            binImage = rotate(binImage);
            width = binImage.length;
            height = binImage[0].length;
        }

        BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        WritableRaster raster = im.getRaster();

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int newVal = binImage[i][j];
                int sample[] = {newVal, newVal, newVal, ExploringMedics.WHITE};
                raster.setPixel(i, j, sample);
            }
        }
        return im;
    }

    private static int[][] rotate(int[][] binImage) {
        int width = binImage.length;
        int height = binImage[0].length;
        int[][] rotated = new int[height][width];

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                rotated[j][i] = binImage[i][j];
            }
        }
        return rotated;
    }
    public final static int circleColor[] = {255, 255, 0, 255};
    public final static int ovalColor[] = {128, 0, 255, 255};
    public final static int mitColor[] = {100, 200, 255, 255};
    public final static int lipidColor[] = {22, 200, 0, 0};

    public static BufferedImage createColorImageFromArray(int[][] origin, int markedImage[][],
            int neiroMarked[][]) {

        int width = markedImage.length;
        int height = markedImage[0].length;
        boolean used[][] = new boolean[width][height];
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                used[i][j] = false;
            }
        }

        BufferedImage im = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        WritableRaster raster = im.getRaster();

        int sample[] = null;

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (used[i][j]) {
                    continue;
                }
                int curVal = origin[i][j];

                if (markedImage[i][j] == CircleClassifier.CIRCLES) {
                    sample = new int[]{(curVal + circleColor[0]) / 2,
                        (curVal + circleColor[1]) / 2,
                        (curVal + circleColor[2]) / 2,
                        ExploringMedics.WHITE};
                } else if (markedImage[i][j] == CircleClassifier.OVAL) {
                    sample = new int[]{(curVal + ovalColor[0]) / 2,
                        (curVal + ovalColor[1]) / 2,
                        (curVal + ovalColor[2]) / 2,
                        ExploringMedics.WHITE};
                } else if (neiroMarked[i][j] == HistogramDescr.MITAH) {
                    sample = new int[]{curVal, curVal, curVal, ExploringMedics.WHITE};

                    boolean isEdge = false;
                    int dx[] = {0, 1, -1};
                    for (int k = 0; k < 3; k++) {
                        for (int p = 0; p < 3; p++) {
                            if (i + dx[k] >= 0 && i + dx[k] < width && j + dx[p] >= 0 && j + dx[p] < height
                                    && neiroMarked[i + dx[k]][j + dx[p]] != HistogramDescr.MITAH) {
                                isEdge = true;
                            }
                        }
                    }

                    if (isEdge) {
                        paintEdge(i, j, origin, used, width, height, raster);
                    }


                } else if (neiroMarked[i][j] == HistogramDescr.LIPID) {
                    sample = new int[]{(curVal + lipidColor[0]) / 2,
                        (curVal + lipidColor[1]) / 2,
                        (curVal + lipidColor[2]) / 2,
                        ExploringMedics.WHITE};
                } else if (neiroMarked[i][j] == HistogramDescr.MARK_EDGE) {
                    paintEdge(i, j, origin, used, width, height, raster);
                } else {
                    sample = new int[]{curVal, curVal, curVal, ExploringMedics.WHITE};
                }

                raster.setPixel(i, j, sample);
                used[i][j] = true;
            }
        }
        return im;
    }

    private static void paintEdge(int i, int j, int[][] origin, boolean used[][], int width, int height, 
            WritableRaster raster) {
        for (int k = i - 2; k <= i + 2; k++) {
            for (int p = j - 2; p <= j + 2; p++) {
                int x = k;
                int y = p;

                if (x >= 0 && x < width && y >= 0 && y < height) {
                    int curVal = origin[x][y];
                    int[] sample = new int[]{(curVal + mitColor[0]) / 2,
                        (curVal + mitColor[1]) / 2,
                        (curVal + mitColor[2]) / 2,
                        ExploringMedics.WHITE};
                    raster.setPixel(x, y, sample);
                    used[x][y] = true;
                }
            }
        }
    }
}
